package cn.edu.ujn.park.controller;


import cn.edu.ujn.park.dao.*;
import cn.edu.ujn.park.service.IParkingRecord;
import cn.edu.ujn.park.service.IParkingSpace;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * @author 29715
 */
@Controller
public class ParkingRecordController {
    @Autowired
    private IParkingRecord parkingRecord;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;
    @Autowired
    private IParkingSpace parkingSpace;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private  RateMapper rateMapper;
    @GetMapping("/parking_record")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, ParkingRecord record){
        record.setStart((page-1)*rows);
        record.setRows(rows);
        Page<ParkingRecord> page1 =parkingRecord.selectAll(record);
        model.addAttribute("page",page1);
        return "parking_record";

    }
    @PostMapping("/record")
    @ResponseBody
    public ResponseEntity<String> select(@RequestParam("license_plate_number") String licenseNumber){
        Vehicle vehicle =vehicleMapper.selectByVehicleName(licenseNumber);
        List<ParkingRecord> records = parkingRecordMapper.selectByRecordId(vehicle.getVehicleId());
        int flag=0;
        for (ParkingRecord record : records) {
            //检测为离开车辆，增加离开时间，并计算费用
            if(record.getExitTime()==null ){
                flag=1;
                record.setExitTime(new Date());
                parkingRecordMapper.updateExit(record);//设置离开时间
                Integer time=parkingRecordMapper.computeTime(record.getRecordId());
                System.out.println(time);//计算停车时间
                ParkingSpace space = parkingSpace.selectByPrimaryKey(record.getSpaceId());
                BigDecimal rate = rateMapper.selectRate(vehicle.getVehicleType(), space.getSpaceType());
                int time1=(time/60)+1;
                BigDecimal timeB=new BigDecimal(time1);
                record.setCost(timeB.multiply(rate));
                parkingRecord.updateByPrimaryKey(record);
                space.setIsAvailable(true);//车辆离开，停车位设置为可用
                parkingSpace.updateByPrimaryKey(space);
                return ResponseEntity.ok("OK");
            }
        }
        //检测为新进入车辆，新增加停车时间
        if (flag==0){
            ParkingRecord newrecord = new ParkingRecord();
            newrecord.setEntryTime(new Date());
            newrecord.setVehicleId(vehicle.getVehicleId());
            newrecord.setUserId(vehicle.getUserId());
            List<ParkingSpace> parkingSpaces = parkingSpace.selectAvailable();//查找空闲停车位
            if(parkingSpaces.isEmpty()){//如果没有空位
                System.out.println("无空闲停车位");
                return ResponseEntity.ok("Busy");
            }else {
                for (ParkingSpace space : parkingSpaces) {
                    //有空闲车位，录入
                    newrecord.setSpaceId(space.getSpaceId());
                    space.setIsAvailable(false);
                    parkingSpace.updateByPrimaryKey(space);//将占用的停车位设置为已占用
                    break;
                }
            }
            parkingRecord.insert(newrecord);
            }
       return ResponseEntity.ok("OK");
    }
    @DeleteMapping("/record/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id){
        int insert = parkingRecord.deleteByPrimaryKey(id);
        return "OK";
    }
}
